using System;
using System.Runtime.InteropServices;
using bitzhuwei.Compiler;

namespace bitzhuwei.JsonFormat {
    /// <summary>
    /// Correspond to the Vn node Json in the grammar(Json).
    /// </summary>
    public partial class Json {
        // [00] Json = Object ;
        // [01] Json = Array ;

        public enum Kind { Object, Array }
        public readonly Kind kind;
        [StructLayout(LayoutKind.Explicit)]
        public struct Union {
            [FieldOffset(0)]
            public List<JsonMember> Object;
            [FieldOffset(0)]
            public List<JsonValue> Array;
        }
        public readonly Union union;

        // [00] Json = Object ;
        public Json(List<JsonMember> r0) {
            this.kind = Kind.Object;
            this.union.Object = r0;
        }

        // [01] Json = Array ;
        public Json(List<JsonValue> r0) {
            this.kind = Kind.Array;
            this.union.Array = r0;
        }

        public override string ToString() {
            string result = "";
            switch (this.kind) {
            case Kind.Object: result = $"{{ {this.union.Object.Count} pairs }}"; break;
            case Kind.Array: result = $"{{ {this.union.Array.Count} values }}"; break;
            default: throw new NotImplementedException();
            }
            return result;
        }
    }
}
